<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { getPatientListApi, deletePatientApi } from '@/api/patient'
  import { useConsultStore } from '@/stores/consult'
  import type { PatientItem } from '@/types/patient'
  import { onShow } from '@dcloudio/uni-app'
  import { ref } from 'vue'

  interface Props {
    type?: 'consult'
  }
  const props = defineProps<Props>()

  if (props.type === 'consult') {
    uni.setNavigationBarTitle({ title: '选择患者' })
  }

  const store = useConsultStore()

  // 2. 判断当前是否为问诊
  const onSelect = (id: string) => {
    if (!props.type) return

    // 3. 只有问诊才能选中id
    store.patientId = id
  }

  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])

  const list = ref<PatientItem[]>([])

  onShow(async () => {
    const res = await getPatientListApi()
    list.value = res
  })

  const onDel = async (id: string, index: number) => {
    await deletePatientApi(id)
    uni.showToast({ title: '删除成功', icon: 'success' })
    list.value.splice(index, 1)

    // 4. 如果删除了选中的患者id, 清空pinia选中的id
    if (id === store.patientId) {
      store.patientId = ''
    }
  }

  const go = () => {
    // TODO:  跳转到预支付界面
    uni.navigateTo({
      url: '/subpkg_consult/payment/payment',
    })
  }
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          :right-options="swipeOptions"
          @click="onDel(item.id, index)"
          v-for="(item, index) in list"
          :key="item.id"
        >
          <view
            @click="onSelect(item.id)"
            :class="{ active: store.patientId === item.id && props.type === 'consult' }"
            class="archive-card"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <!-- idCard.replace(/^(\d{3})\d{11}(\d{4})$/, "$1***********$2"); -->
              <text class="id-card">{{
                item.idCard.replace(/^(\d{3})\d{11}(\d{4})$/, '$1***********$2')
              }}</text>
              <text class="default" v-if="item.defaultFlag">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.gender ? '男' : '女' }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/form?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="list.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>

      <!-- 2. 准备按钮 下一步 cv -->
      <view v-if="type === 'consult' && store.patientId !== ''">
        <button @click="go" class="uni-button">下一步</button>
      </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './list.scss';
</style>
